package com.zhiqingchun.dao.contact.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zhiqingchun.dao.BaseDao;
import com.zhiqingchun.dao.contact.ContactResult;
import com.zhiqingchun.dao.contact.IContactDao;
import com.zhiqingchun.pojo.UsrContactDO;

public class ContactDao extends BaseDao implements IContactDao {

	@Override
	public ContactResult byId(UsrContactDO usrContactDO) {
		String sql = "select c.*, u.usr from usr_contact c, usr u "
				+ "where u.id = c.contact_id";
		if (usrContactDO != null && usrContactDO.getUserId() != null) {
			sql += " and c.user_id = ?";
		}
		if (usrContactDO != null && usrContactDO.getContactId() != null) {
			sql += " and c.contact_id = ?";
		}
		sql += " order by c.insert_time";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			if (usrContactDO != null && usrContactDO.getUserId() != null) {
				ps.setInt(1, usrContactDO.getUserId());
			}
			if (usrContactDO != null && usrContactDO.getContactId() != null) {
				ps.setInt(2, usrContactDO.getContactId());
			}
			ResultSet rs = ps.executeQuery();
			List<UsrContactDO> contactList = new ArrayList<UsrContactDO>();
			while (rs.next()) {
				UsrContactDO usrContactDO2 = new UsrContactDO();
				usrContactDO2.setUserId(rs.getInt("user_id"));
				usrContactDO2.setContactId(rs.getInt("contact_id"));
				usrContactDO2.setContactName(rs.getString("usr"));
				contactList.add(usrContactDO2);
			}
			
			ContactResult contactResult = new ContactResult();
			contactResult.setContactList(contactList);
			contactResult.setSuccess(true);
			return contactResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

	@Override
	public ContactResult add(UsrContactDO usrContactDO) {
		String sql = "insert into usr_contact (user_id, contact_id, insert_time) values (?, ?, ?)";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, usrContactDO.getUserId());
			ps.setInt(2, usrContactDO.getContactId());
			ps.setDate(3, new java.sql.Date( new Date().getTime()));
			ps.executeUpdate();
			ps.close();
			
			ContactResult contactResult = new ContactResult();
			contactResult.setSuccess(true);
			return contactResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

	@Override
	public ContactResult remove(UsrContactDO usrContactDO) {
		String sql = "delete from usr_contact where user_id = ? and contact_id = ?";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, usrContactDO.getUserId());
			ps.setInt(2, usrContactDO.getContactId());
			ps.executeUpdate();
			ps.close();
			
			ContactResult contactResult = new ContactResult();
			contactResult.setSuccess(true);
			return contactResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

}
